package com.zzyl.controller;

import com.aliyun.iot20180120.models.DeleteDeviceRequest;
import com.aliyun.iot20180120.models.QueryDeviceDetailRequest;
import com.aliyun.iot20180120.models.QueryDevicePropertyStatusRequest;
import com.aliyun.iot20180120.models.QueryThingModelPublishedRequest;
import com.zzyl.base.PageResponse;
import com.zzyl.base.ResponseResult;
import com.zzyl.dto.DeviceDto;
import com.zzyl.dto.DevicePageQueryDto;
import com.zzyl.service.DeviceService;
import com.zzyl.service.FloorService;
import com.zzyl.vo.DeviceVo;
import com.zzyl.vo.FloorVo;
import com.zzyl.vo.ProductVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@Slf4j
@RestController
@RequestMapping("/iot")
@Api(tags = "管理端 - 物联网iot相关接口")
public class DeviceController {
    @Autowired
    private DeviceService deviceService;
    @Autowired
    private FloorService floorService;

    @PostMapping("/syncProductList")
    @ApiOperation(value = "从物联网平台同步产品列表")
    @Transactional
    public ResponseResult<Void> syncProductList() {
        log.info("从物联网平台同步产品列表...");
        //调用业务层方法实现IOT平台产品列表同步
        deviceService.syncProductList();
        //返回数据
        return ResponseResult.success();
    }

    /**
     * 查询所有产品列表
     */
    @GetMapping("/allProduct")
    @ApiOperation(value = "查询所有产品列表")
    @Transactional
    public ResponseResult<List<ProductVo>> allProduct() {
        log.info("查询所有产品列表...");
        //调用业务层方法实现IOT平台产品列表同步
        List<ProductVo> productVoList = deviceService.allProduct();
        //返回数据
        return ResponseResult.success(productVoList);
    }

    /**
     * 注册设备
     */
    @PostMapping("/RegisterDevice")
    @ApiOperation(value = "注册设备")
    public ResponseResult<Void> registerDevice(@RequestBody DeviceDto deviceDto) {
        log.info("注册设备...", deviceDto);
        //调用业务层方法实现IOT平台产品列表同步
        ResponseResult<Void> zhuce = deviceService.registerDevice(deviceDto);
        //返回数据
        return ResponseResult.success(zhuce);
    }

    /**
     * 分页
     */
    @GetMapping("/pageQueryDevice")
    @ApiOperation(value = "分页查询设备列表")
    public ResponseResult<PageResponse<DeviceVo>> pageQueryDevice(DevicePageQueryDto devicePageQueryDto) {
        PageResponse<DeviceVo> deviceVoPageResponse = deviceService.pageQueryDevice(devicePageQueryDto);
        return ResponseResult.success(deviceVoPageResponse);
    }

    /**
     * 查询设备详情
     */
    @PostMapping("/QueryDeviceDetail")
    @ApiOperation(value = "查询设备详细数据")
    public ResponseResult<DeviceVo> queryDeviceDetail(@RequestBody QueryDeviceDetailRequest request) {
        //调用业务层方法实现IOT平台产品列表同步
        DeviceVo deviceVo = deviceService.queryDeviceDetail(request);
        return ResponseResult.success(deviceVo);
    }

    /**
     * 查询指定设备的物模型运行状态
     */
    @PostMapping("/QueryDevicePropertyStatus")
    @ApiOperation(value = "查询指定设备的物模型运行状态")
    public ResponseResult queryDevicePropertyStatus(@RequestBody QueryDevicePropertyStatusRequest request) {
        log.info("查询指定设备的物模型运行状态...", request);
        return deviceService.queryDevicePropertyStatus(request);
    }

    /**
     * 查看指定产品的已发布物模型中的功能定义详情
     */
    @PostMapping("/QueryThingModelPublished")
    @ApiOperation(value = "查看指定产品的已发布物模型中的功能定义详情")
    public ResponseResult queryThingModelPublished(@RequestBody QueryThingModelPublishedRequest request) {
        log.info("查看指定产品的已发布物模型中的功能定义详情...", request);
        return deviceService.queryThingModelPublished(request);
    }


    /**
     * 修改设备备注名称
     */
    @PostMapping("/UpdateDevice")
    @ApiOperation(value = "修改设备备注名称")
    public ResponseResult<DeviceDto> updateDeviceNickName(@RequestBody DeviceDto deviceDto) {
        log.info("修改设备备注名称...", deviceDto);
        deviceService.updateDevice(deviceDto);
        return ResponseResult.success(deviceDto);
    }

    /**
     * 删除设备
     */
    @DeleteMapping("/DeleteDevice")
    @ApiOperation(value = "删除设备")
    public ResponseResult<Void> deleteDevice(@RequestBody DeleteDeviceRequest request) {
        log.info("删除设备...", request);
        deviceService.deleteDevice(request);
        return ResponseResult.success();
    }

    /**
     * "查询所有楼层（智能设备）
     */
    @GetMapping("/floor/getAllFloorsWithDevice")
    @ApiOperation("查询所有楼层（智能设备）")
    public ResponseResult<List<FloorVo>> getAllFloorsWithDevice() {
        List<FloorVo> list = floorService.getAllFloorsWithDevice();
        return ResponseResult.success(list);
    }
}